package com.voiceblog.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.support.PagedListHolder;

import com.voiceblog.model.Blog;
import com.voiceblog.model.FriendDAO;
import com.voiceblog.model.Users;
import com.voiceblog.model.Friend;

public class FriendList extends BaseAction {

	private Users user;
	private String account;
	private String discription;
	

	private List<Blog> list;
	private PagedListHolder blogList;
	private List<Friend> friendList;
	private List<Users> friends;


	public String list() throws Exception {

		//user = getDao().findById(account);
		 user=(Users)getSession().get("user");
		if(user!=null){
		friendList = getDao().findFriend(10, user);
		friends=new ArrayList();
		for(Friend user :friendList){
			Users users=this.getDao().findById(user.getFriendAccount());
			friends.add(users);
		}   
		return SUCCESS;
	}else{
		return ERROR;
	}
	}
	

	


	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public List<Friend> getFriendList() {
		return friendList;
	}

	public void setFriendList(List<Friend> friendList) {
		this.friendList = friendList;
	}



	public List<Blog> getList() {
		return list;
	}

	public void setList(List<Blog> list) {
		this.list = list;
	}

	public PagedListHolder getBlogList() {
		return blogList;
	}

	public void setBlogList(PagedListHolder blogList) {
		this.blogList = blogList;
	}



	public List<Users> getFriends() {
		return friends;
	}

	public void setFriends(List<Users> friends) {
		this.friends = friends;
	}
	public String getDiscription() {
		return discription;
	}





	public void setDiscription(String discription) {
		this.discription = discription;
	}
	private FriendDAO friendDAO;
	public FriendDAO getFriendDAO() {
		return friendDAO;
	}
	public void setFriendDAO(FriendDAO friendDAO) {
		this.friendDAO = friendDAO;
	}
	//delete a friend
	public String deletefriends(){
		
		friendDAO.deleteFriend(((Users)getSession().get("blogUser")).getAccount(),account);
		 return SUCCESS;
		}
	//add a friend
	public String addfriend(){
		Friend friend=new Friend();
		friend.setFriendAccount(account);
		friend.setFriendDescription(discription);
		friend.setUsers((Users)getSession().get("blogUser"));
		if(friendDAO.find(((Users)getSession().get("blogUser")).getAccount(), account).size()>0){
			return INPUT;
		}
		friendDAO.save(friend);
		return SUCCESS;
	}
}
